package user_interface;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;

import myutil.GetTable;

public class User {
	// ok
	public static boolean register(String[] userinfo)// username(r),password(r),nickname,email,type;
	{
		if (!checkUsername(userinfo[0]))
			return false;
		else {
			GetTable gt = new GetTable("userinfo");
			HTable ht = gt.ht;
			byte[] rowid = Bytes.toBytes(userinfo[0]);
			byte[] family = Bytes.toBytes("metainfo");

			Put put = new Put(rowid).add(family, Bytes.toBytes("password"),
					Bytes.toBytes(userinfo[1]));

			// ///////////////////////////////////////////////////////////////////////////////////
			if (!userinfo[2].isEmpty())
				put.add(family, Bytes.toBytes("nickname"),
						Bytes.toBytes(userinfo[2]));
			if (!userinfo[3].isEmpty())
				put.add(family, Bytes.toBytes("email"),
						Bytes.toBytes(userinfo[3]));
			if (!userinfo[4].isEmpty())
				put.add(family, Bytes.toBytes("type"), Bytes.toBytes("0"));

			try {
				ht.put(put);
				return true;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			} finally {
				gt.CloseTable();
			}
		}
	}

	// ok
	public static boolean checkUsername(String username) {
		GetTable gt = new GetTable("userinfo");
		HTable ht = gt.ht;
		Get g = new Get(Bytes.toBytes(username));
		try {
			Result r = ht.get(g);
			if (r.size() == 0)
				return true;
			else
				return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			gt.CloseTable();
		}
	}

	// ok
	public static boolean uploadHead(String username, String password, byte[] dh) {
		if (!checkLogin(username, password))
			return false;
		else {
			GetTable gt = new GetTable("userinfo");
			HTable ht = gt.ht;
			byte[] rowid = Bytes.toBytes(username);
			byte[] family = Bytes.toBytes("metainfo");
			Put put = new Put(rowid).add(family, Bytes.toBytes("head"), dh);
			try {
				ht.put(put);
				return true;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			} finally {
				gt.CloseTable();
			}
		}
	}

	// ok
	public static byte[] getHead(String username, String password) {
		if (!checkLogin(username, password))
			return null;
		else {
			GetTable gt = new GetTable("userinfo");
			HTable ht = gt.ht;
			Get g = new Get(Bytes.toBytes(username));
			g.addColumn(Bytes.toBytes("metainfo"), Bytes.toBytes("head"));
			try {
				Result r = ht.get(g);
				return r.value();

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			} finally {
				gt.CloseTable();
			}
		}
	}

	// ok
	public static ArrayList<String> login(String username, String password)// ;nickname,email,type,currentweibo
	{
		if (!checkLogin(username, password))
			return null;
		GetTable gt = new GetTable("userinfo");
		HTable ht = gt.ht;
		ArrayList<String> al = new ArrayList<String>();
		Get g = new Get(Bytes.toBytes(username));
		try {
			Result r = ht.get(g);
			al.add(Bytes.toString(r.getValue(Bytes.toBytes("metainfo"),
					Bytes.toBytes("nickname"))));
			al.add(Bytes.toString(r.getValue(Bytes.toBytes("metainfo"),
					Bytes.toBytes("email"))));
			al.add(Bytes.toString(r.getValue(Bytes.toBytes("metainfo"),
					Bytes.toBytes("type"))));
			al.add(Bytes.toString(r.getValue(Bytes.toBytes("metainfo"),
					Bytes.toBytes("currentweibo"))));
			return al;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			gt.CloseTable();
		}

	}

	// ok
	public static boolean checkLogin(String username, String password) {
		GetTable gt = new GetTable("userinfo");
		HTable ht = gt.ht;
		Get g = new Get(Bytes.toBytes(username));
		g.addColumn(Bytes.toBytes("metainfo"), Bytes.toBytes("password"));
		try {
			Result r = ht.get(g);
			// System.out.println(Bytes.toString(r.value()));
			if (Bytes.toString(r.value()).endsWith(password))
				return true;
			else
				return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList<String> al = new ArrayList<String>();
		al.add("liutaojason3");
		al.add("1428570003");
		al.add("JAsona3");
		al.add("liutao891017@hotmailcom3");
		al.add("0");
		// User.register(al);
		 System.out.println(User.checkUsername("lyl"));
		// System.out.println(User.checkLogin("liutaojason3","1428570003"));
		// System.out.println(User.checkLogin("liutaojason", "142857000"));
		// System.out.println(User.uploadHead("liutaojason","142857000",
		// Bytes.toBytes("myhead")));
		// System.out.println(User.login("liutaojason2","1428570002").get(0));
		// ArrayList<String> al2 = User.login("liutaojason", "142857000");
		// System.out.println(al2.get(0)+al2.get(1));
	}

}
